<template>
  <div style="position: relative; height: 420px;">
    <div style="width: 200px; height: 400px; display: inline-block; padding: 10px; position: absolute; left: 0">
      <!-- 版本号 -->
      <div style="width: 100%; height: 100%; overflow: auto;"> <!-- 版本号内容区域 -->
        <el-radio-group v-model="selectedVersion">
          <div v-for="(knowledge, index) in knowledgeList" :key="index" style="width: 200px; height: 50px;">
            <!-- 一条版本号 -->
            <div style="display: inline-block; position: relative;"> <!-- 左边部分 -->
              <div style="position: absolute;">
                <el-radio :label="knowledge.historyId">版本号：<span>{{knowledge.version}}</span></el-radio>
              </div>
              <div style="position: absolute; top: 18px; left: 6px;">
                <div v-if="index < knowledgeList.length - 1"
                     style="width: 1px; height: 30px; background-color: #e4e4e4;"></div>
              </div>
              <div style="position: absolute; font-size: 10px; width: 150px; top: 20px; left: 23px; color: #C9C9C9">
                {{knowledge.date}}
              </div>
            </div>
          </div>
        </el-radio-group>
      </div>
    </div>
    <div
      style="border: 1px solid #e4e4e4; display: inline-block; width: 730px; height: 400px; padding: 10px; position: absolute; right: 0; overflow: auto;">
      <div id="context" style="background-color: #F0F8F8; padding: 30px;">
        <div style="width: 100%; text-align: center;">
          <span class="knowledgeName" v-html="this.knowledgeContent.name"></span>
        </div>
        <div v-for="field in this.knowledgeContent.fields" style="margin-bottom: 20px;">
          <a :name="field.name"></a>
          <div>
            <span class="fieldName">{{ field.name }}</span>
          </div>
          <div id="content" v-html="field.content"></div>
        </div>
      </div>
    </div> <!-- 知识内容 -->
  </div>
</template>

<script>
export default {
  name: 'KnowledgeHistory',
  props: {
    knowledgeId: String
  },
  data() {
    return {
      knowledgeList: [],
      knowledgeContent: '',
      selectedVersion: ''
    }
  },
  watch: {
    selectedVersion(nVal) {
      // Get knowledge content
      this.axios("/pmph/action/knowledgeLib/selectHistoryKnowledgeContent", {
        params: {
          historyId: nVal
        }
      }).then((res) => {
        this.knowledgeContent = res.data
      }).catch((err) => {
        this.$message.error("获取知识内容的时候失败")
        console.error(err)
      })
    },
    knowledgeId(nVal) {
      this.restore(nVal)
    }
  },
  methods: {
    restore(knowledgeId) {
      // Get knowledge history data
      this.axios.get("/pmph/action/knowledgeLib/selectKnowledgeHistory", {
        params: {
          id: knowledgeId
        }
      }).then((res) => {
        this.knowledgeList = res.data
        this.knowledgeContent = {}
        if (res.data.length === 0) {
          this.$message.warning('没有版本历史记录')
          return;
        }
        this.selectedVersion = this.knowledgeList[0].historyId
      })
    }
  },
  created() {
    this.restore(this.knowledgeId)
  }
}
</script>

<style scoped>
#content >>> .table,
#content >>> .general,
#content >>> .picture,
#content >>> .cover,
#content >>> .formula,
#content >>> .flow,
#content >>> .default,
#content >>> .other {
  width: 50%;
  height: auto;
  margin: 0 auto;
  display: block;
}

.knowledgeName {
  font-family: MicrosoftYaHei-Bold, sans-serif;
  font-size: 32px;
  color: #333333;
  letter-spacing: 0;
}

.fieldName {
  font-family: MicrosoftYaHei-Bold, sans-serif;
  font-size: 24px;
  color: #333333;
  letter-spacing: 0;
}
</style>
